package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Comment;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.CommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper contentMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail) {
        if (strategyDetail.getSequence()==null){
            int maxSequence = strategyDetailMapper.selectMaxSequence(strategyDetail.getCatalog().getId());
            strategyDetail.setSequence(maxSequence+1);
        }

        if (strategyDetail.getState()==true){
            strategyDetail.setReleaseTime(new Date());
        }
        
        if(strategyDetail.getId()!=null){
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            strategyDetail.getStrategyContent().setId(strategyDetail.getId());
            contentMapper.updateByPrimaryKey(strategyDetail.getStrategyContent());
        }else{
            strategyDetailMapper.insert(strategyDetail);
            //给文章对象设置id
            strategyDetail.getStrategyContent().setId(strategyDetail.getId());
            contentMapper.insert(strategyDetail.getStrategyContent());
        }
    }

    @Override
    public StrategyContent selectContentById(Long id) {
        //查询攻略文章
        return contentMapper.selectByPrimaryKey(id);
    }
    @Override
    public StrategyDetail selectById(Long id) {
        //查询攻略文章
        StrategyContent content = contentMapper.selectByPrimaryKey(id);
        //查询攻略文章内容
        StrategyDetail detail = strategyDetailMapper.selectByPrimaryKey(id);
        //根据id查询评论数量并设置给detail对象
        detail.setCommentNum(commentMapper.selectForNum(detail.getId(), Comment.MODELSTATE_STRATEGYDETAILS));
        //设置攻略文章内容
        detail.setStrategyContent(content);
        //查询点赞数量
        int likeNum = strategyDetailMapper.queryLikeCountByStrategyDetailsId(id);
        //设置点赞数量
        detail.setLikeNum(likeNum);
        //查询当前用户是否已经点赞
        //获取当前用户的id
        Long uid = UserContext.getCurrentUser().getId();
        //查询当前用户是否已点赞
        int isLike = strategyDetailMapper.queryIsLike(uid, id);
        //处理查询结果
        if (isLike==1){
            detail.setLike(true);
        }else {
            detail.setLike(false);
        }
        return detail;
    }


    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo(list);
    }

}
